package com.example.app2024.wechatr;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.app2024.R;

import java.util.ArrayList;
import java.util.List;

public class WechatActivity extends AppCompatActivity
{
      private ViewPager viewPager;
      private ImageView page0;
      private ImageView page1;
      private ImageView page2;
      private ImageView page3;
      private ImageView page4;
      private ImageView page5;

      private List<View> viewList = new ArrayList<>();

      private boolean isPress;
      private  boolean isOpen;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_wechat);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            initViews();
            addViews();
            viewPager.setAdapter(new MyPagerAdatper());
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
            {
                  @Override
                  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
                  {
                        if (isPress && viewList.size()-1 == position && !isOpen &&  positionOffsetPixels ==0)
                        {
                              isOpen = true;
                              Intent intent =  new Intent(WechatActivity.this, chatActivity.class);
                              startActivityForResult(intent , 1);
                        }

                  }

                  @Override
                  public void onPageSelected(int position)
                  {
                        switch (position)
                        {
                              case  0:
                                    page0.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                                    page1.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    break;
                              case  1:
                                    page1.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                                    page0.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    page2.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    break;
                              case  2:
                                    page2.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                                    page1.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    page3.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    break;
                              case  3:
                                    page3.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                                    page2.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    page4.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    break;
                              case  4:
                                    page4.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                                    page3.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    page5.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    break;
                              case  5:
                                    page5.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                                    page4.setImageDrawable(getResources().getDrawable(R.drawable.page_no));
                                    break;

                        }

                  }

                  @Override
                  public void onPageScrollStateChanged(int state)
                  {
                        if (state == viewPager.SCROLL_STATE_DRAGGING)
                        {
                              isPress = true;
                        }else {
                              isPress = false;
                        }

                  }
            });
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
      {
            super.onActivityResult(requestCode, resultCode, data);
            isOpen = false;
      }

      private  class  MyPagerAdatper extends PagerAdapter
      {

            @Override
            public int getCount()
            {
                  return viewList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
            {
                  return view == object;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object)
            {
                  container.removeView(viewList.get(position));
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position)
            {
                 container.addView(viewList.get(position));
                 return  viewList.get(position);
            }
      }

      private void addViews()
      {
            LayoutInflater inflater = LayoutInflater.from(this);
            View view1 = inflater.inflate(R.layout.whats1, null);
            View view2 = inflater.inflate(R.layout.whats2, null);
            View view3 = inflater.inflate(R.layout.whats3, null);
            View view4 = inflater.inflate(R.layout.whats4, null);
            View view5 = inflater.inflate(R.layout.whats5, null);
            View view0 = inflater.inflate(R.layout.whats0, null);

            viewList.add(view0);
            viewList.add(view1);
            viewList.add(view2);
            viewList.add(view3);
            viewList.add(view4);
            viewList.add(view5);

      }

      private void initViews()
      {
            viewPager = findViewById(R.id.vp_whatsnew);
            page0 = findViewById(R.id.iv_page0);  //6个小圆点
            page1 = findViewById(R.id.iv_page1);
            page2 = findViewById(R.id.iv_page2);
            page3 = findViewById(R.id.iv_page3);
            page4 = findViewById(R.id.iv_page4);
            page5 = findViewById(R.id.iv_page5);

      }


}